<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\YiJingUpdate;
use App\YiJing;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class YiJingController extends BaseController
{
    /**
     * @param Request $request
     * @return mixed
     * status  false   0-未评估 1-已评估
     * name    false    名称
     */
    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : $this->orderBy;
        $orderType = $request->order_type ? $request->order_type : $this->orderType;
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        $lists = YiJing::orderBy($orderBy, $orderType)->with(['user']);
        if ($request->get('status')) {
            $lists = $lists->where('status', $request->status);
        }
        if ($request->get('name')) {
            $lists = $lists->where('name', 'like', '%' . $request->name . '%');
        }
        return $lists->paginate($pageSize);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //限制。。。。。
        if (YiJing::where('user_id', Auth::id())->where('status', 0)->first()) {
            return response(['message' => '请勿重复提交'], 400);
        }
        $res = YiJing::create(array_merge($request->only([
            'name',
            'sex',
            'birth_address',
            'lunar_calendar',
            'gregorian_calendar',
            'zhongdian',
        ]), ['user_id' => Auth::id()]));
        if ($res) {
            response(['message' => '提交成功']);
        }
        response(['message' => '提交失败'], 400);
    }

    /**
     * @param YiJing $yiJing
     * @return \Illuminate\Database\Eloquent\Model|null|object|static
     */
    public function show($yiJing)
    {
        return YiJing::where('id', $yiJing)->with(['user'])->first();
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\YiJing $yiJing
     * @return \Illuminate\Http\Response
     */
    public function edit(YiJing $yiJing)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\YiJing $yiJing
     * @return \Illuminate\Http\Response
     */
    public function update(YiJingUpdate $request, YiJing $yiJing)
    {
        $yiJing->update(array_merge($request->only([
            'work_fraction',
            'innovate_fraction',
            'stabilize_fraction',
            'marriage_fraction',
            'fitness_fraction',
            'income_fraction',
            'capacity_fraction',
            'work_proposal',
            'innovate_proposal',
            'stabilize_proposal',
            'marriage_proposal',
            'fitness_proposal',
            'income_proposal',
            'capacity_proposal',
            'else_proposal'
        ]), ['status' => 1]));
        return response(['message' => '评估成功']);
    }

    /**
     * @param YiJing $yiJing
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     * 用户自己删除自己的评估结果
     */
    public function destroy(YiJing $yiJing)
    {
        $user_id = Auth::id();
        if ($yiJing->user_id != $user_id) {
            return response(['message' => '无删除权限'], 400);
        }
        $yiJing->delete();
        return response(['message' => '删除成功']);
    }

    /**
     * @param Request $request
     * @return mixed
     * sign    false   默认获取最新评估结果 sign==history 获取历史评估结果
     */
    public function getMyYiJing(Request $request)
    {
        $user_id = Auth::id();
        $data = YiJing::orderBy('id', 'desc')->where('user_id', $user_id)->where('status',1)->first(); //获取最新评估
        if ($request->sign == 'history') {
            $data = YiJing::orderBy('id', 'desc')->where('user_id', $user_id)->get();  //历史记录
        }
        if ($request->id) {
            $data = YiJing::where('id', $request->id)->first();
        }
        return $data;
    }
}
